﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using YhmOA.Common;
using YhmOA.Model;
using YhmOA.IDAL;
using System.Reflection;

namespace YhmOA.Commons
{
    public partial class SelectItem : BasePage
    {
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            string typeName = Request.QueryString["data"] ?? "TopMenuSelectLeftTop";
            string[] showColumns = null;
            string[] bindColumns = null;
            switch (typeName)
            {
                case "TopMenuSelectLeftTop":
                    showColumns = new string[] { "菜单名称" };
                    bindColumns = new string[] { "Name" };
                    break;
            }
            for (int i = 0; i < showColumns.Length; i++)
            {
                TemplateField tf = new TemplateField();
                tf.ShowHeader = true;
                tf.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, showColumns[i]);
                tf.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, bindColumns[i]);
                this.gvShow.Columns.Add(tf);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetSource();
            }
        }

        [AjaxMethod]
        public void GetSource()
        {
            string typeName = Request.QueryString["data"] ?? "TopMenuSelectLeftTop";
            string searchText = this.txt_Search.Text;
            int count;
            switch (typeName)
            { 
                case "TopMenuSelectLeftTop":
                    List<ManagerOA_Menu_LeftMenu> list = new List<ManagerOA_Menu_LeftMenu>();
                    list = DataProvider.GetEFDal<IManagerOA_Menu_LeftMenuDAL>().GetManagerOA_Menu_LeftMenuByTopMenuID(Convert.ToInt32(Request.QueryString["topID"] ?? "11"), searchText);
                    ViewState["DataSource"] = list;
                    this.gvShow.DataSource = list;
                    this.gvShow.AutoGenerateColumns = false;
                    this.gvShow.DataBind();
                    break;
            }
        }

        #region 获取回调函数的名称
        /// <summary>
        /// 获取回调函数的名称
        /// </summary>
        /// <returns></returns>
        [AjaxMethod]
        public string GetCallFun()
        {
            return Request.QueryString["call"] ?? "CallFun";
        }
        #endregion

        #region 获取选中的值
        /// <summary>
        /// 返回选中的值
        /// </summary>
        /// <returns></returns>
        [AjaxMethod]
        public string GetReturnValue()
        {
            string returnValue = "";
            string typeName = Request.QueryString["data"] ?? "TopMenuSelectLeftTop";
            string[] columns = null;
            switch (typeName)
            {
                case "TopMenuSelectLeftTop":
                    columns = new string[] { "ID", "Name" };
                    break;
            }
            string str = Request.Form["ItemChk"] ?? "";
            if (str != "")
            {
                str = "_" + str + "_";
                IList dataSource = ViewState["DataSource"] as IList;
                foreach (object item in dataSource)
                {
                    PropertyInfo ID = item.GetType().GetProperty("ID");
                    string IDValue = "_" + ID.GetValue(item, null).ToString() + "_";
                    if (str.IndexOf(IDValue) > -1)
                    {
                        foreach (string strColumn in columns)
                        {
                            PropertyInfo pi = item.GetType().GetProperty(strColumn);
                            returnValue = returnValue + pi.GetValue(item, null).ToString() + "#";
                        }
                        returnValue = returnValue.Substring(0, returnValue.Length - 1);
                        returnValue = returnValue + ",";
                    }
                }
            }
            if (returnValue.Length > 0)
                returnValue = returnValue.Substring(0, returnValue.Length - 1);
            return returnValue;
        }
        #endregion

        protected void gvShow_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("oncontextmenu", "ExecReturnValue();return false;");
            }
        }
    }

    public class GridViewTemplate : ITemplate
    {
        private DataControlRowType templateType;
        private string columnName;

        public GridViewTemplate(DataControlRowType type, string colname)
        {
            templateType = type;
            columnName = colname;
        }

        public void InstantiateIn(Control container)
        {
            switch (templateType)
            {
                case DataControlRowType.Header:
                    Literal lc = new Literal();
                    lc.Text = "<B>" + columnName + "</B>";
                    container.Controls.Add(lc);
                    break;
                case DataControlRowType.DataRow:
                    Literal ltTxt = new Literal();
                    ltTxt.ID = columnName;
                    ltTxt.DataBinding += new EventHandler(this.ltTxt_DataBinding);
                    container.Controls.Add(ltTxt);
                    break;
                default:
                    break;
            }
        }

        private void ltTxt_DataBinding(Object sender, EventArgs e)
        {
            Literal l = (Literal)sender;
            GridViewRow row = (GridViewRow)l.NamingContainer;
            l.Text = DataBinder.Eval(row.DataItem, l.ID).ToString();
        }
    }
}